<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>nagios 监控客户端 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>nagios 监控客户端</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Nagios/">Nagios</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在客户端机器阿航安装 epel 扩展源</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum install -y epel-release</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;安装 nagios 以及 nagios-plugins</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum install -y nagios-plugins nagios-plugins-all nrpe nagios-plugins-nrpe</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vim /etc/nagios/nrpe.cfg</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;找到 “allowed_hosts=127.0.0.1” 改为 “allowed_hosts=127.0.0.1，192.168.0.62 ” 后面的 ip 为服务端 ip ； 找到 “dont_blame_nrpe=0” 改为 “dont_blam_nrpe=1” </p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nagios%E7%9B%91%E6%8E%A7%E5%AE%A2%E6%88%B7%E7%AB%AF/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改为</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nagios%E7%9B%91%E6%8E%A7%E5%AE%A2%E6%88%B7%E7%AB%AF/02.png?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nagios%E7%9B%91%E6%8E%A7%E5%AE%A2%E6%88%B7%E7%AB%AF/03.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改为</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nagios%E7%9B%91%E6%8E%A7%E5%AE%A2%E6%88%B7%E7%AB%AF/04.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动客户端</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/etc/init.d/nrpe start</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;客户端配置好以后，在到服务端继续配置。客户端 ip 为 192.168.0.98，下面定义子配置文件。（以下操作在服务端上完成）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@nagios ~]<span class="comment"># cd /etc/nagios/conf.d/</span></div><div class="line">[root@nagios conf.d]<span class="comment"># vim /192.168.0.98.cfg</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增加配置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div></pre></td><td class="code"><pre><div class="line">define host&#123;</div><div class="line">    use linux-server</div><div class="line">    host_name 192.168.0.98</div><div class="line">    <span class="built_in">alias</span> 0.98</div><div class="line">    address 192.168.0.98</div><div class="line">&#125;</div><div class="line"></div><div class="line">define service&#123;</div><div class="line">    use generic-service</div><div class="line">    host_name 192.168.0.98</div><div class="line">    service_description check_ping</div><div class="line">    check_command check_ping!100.0,20%!200.0,50%</div><div class="line">    max_check_attempts 5</div><div class="line">    normal_check_interval 1</div><div class="line">&#125;</div><div class="line"></div><div class="line">define service&#123;</div><div class="line">    use generic-service</div><div class="line">    host_name 192.168.0.98</div><div class="line">    service_description check_ssh</div><div class="line">    check_command check_ssh</div><div class="line">    max_check_attempts 5 </div><div class="line">    normal_check_interval 1 </div><div class="line">    notification_interval 60</div><div class="line">&#125;</div><div class="line"></div><div class="line">define service&#123;</div><div class="line">    use generic-service</div><div class="line">    host_name 192.168.0.98</div><div class="line">    service_description check_http</div><div class="line">    check_command check_http</div><div class="line">    max_check_attempts 5</div><div class="line">    normal_check_interval 1</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：</p>
<ul>
<li>“max_check_attempts  5” ：表示当nagios检测到问题时，一共尝试检测5次都有问题才会告警，如果该数值为1，那么检测到问题立即告警</li>
<li>“normal_check_interval 1”：表示重新检测的时间间隔，单位是分钟，默认是3分钟</li>
<li>“notification_interval 60”：表示在服务出现异常后，故障一直没有解决，nagios再次对使用者发出通知的时间。单位是分钟。如果你认为，所有的事件只需要一次通知就够了，可以把这里的选项设为0。</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上服务不依赖于客户端 nrpe 服务，可以想象，在子机电脑上可以使用 ping 或者 telnet 探测远程任何一台机器是否存活、是否开启某个端口或服务。而当想要检测客户端上的某个具体服务的情况时，就需要借助于 nrpe 了，比如想知道客户端机器的负载或磁盘使用情况。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑完成配置文件后，检测配置是否有错</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@nagios conf.d]<span class="comment"># nagios -v /etc/nagios/nagios.cfg</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再服务端重启一下 nagios 服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@nagios conf.d]<span class="comment"># service nagios restart</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后在浏览器里访问 nagios ，刷新会发现多出来一台主机，并且多出来三个服务。只不过这三个服务并不是我们想要的，想要监控系统负载，监控磁盘使用率等服务，这时候就要使用 nrpe 服务了。继续在服务端上添加服务。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@nagios ~]<span class="comment"># vim /etc/nagios/objects/commands.cfg</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增加</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">define <span class="built_in">command</span>&#123;</div><div class="line">    command_name check_nrpe</div><div class="line">    command_line <span class="variable">$USER1</span>$/check_nrpe -H <span class="variable">$HOSTADDRESS</span>$ -c <span class="variable">$ARG1</span>$</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后继续编辑</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@nagios conf.d]<span class="comment"># vim 192.168.0.98.cfg</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line">define service&#123;</div><div class="line">    use generic-service</div><div class="line">    host_name 192.168.0.98</div><div class="line">    service_description check_load</div><div class="line">    check_command check_nrpe!check_load</div><div class="line">    max_check_attempts 5</div><div class="line">    normal_check_interval 1</div><div class="line">&#125;</div><div class="line"></div><div class="line"></div><div class="line">define service&#123;</div><div class="line">    use generic-service</div><div class="line">    host_name 192.168.0.98</div><div class="line">    service_description check_disk_sda1</div><div class="line">    check_command check_nrpe!check_sda1</div><div class="line">    max_check_attempts 5</div><div class="line">    normal_check_interval 1</div><div class="line">&#125;</div><div class="line"></div><div class="line">define service&#123;</div><div class="line">    use generic-service</div><div class="line">    host_name 192.168.0.98</div><div class="line">    service_description check_disk_sda3</div><div class="line">    check_command check_nrpe!check_sda3</div><div class="line">    max_check_attempts 5</div><div class="line">    normal_check_interval 1</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：“check_nrpe!check_load” 这里的 check_nrpe 就是在 commands.cfg 刚刚定义的，check_load 是远程主机上的一个检测脚本。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在远程主机上编辑 nrpe.cfg 配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@nagios conf.d]<span class="comment"># vim /etc/nagios/nrpe.cfg</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;搜索 check_load ，这行就是在服务器上要执行的脚本了。然后把 check_hda1 更改一下：/dev/hds1 改为 ，/dev/sda1 。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nagios%E7%9B%91%E6%8E%A7%E5%AE%A2%E6%88%B7%E7%AB%AF/05.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改为</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nagios%E7%9B%91%E6%8E%A7%E5%AE%A2%E6%88%B7%E7%AB%AF/06.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再加一行</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nagios%E7%9B%91%E6%8E%A7%E5%AE%A2%E6%88%B7%E7%AB%AF/07.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;客户端上重启一下 nrpe 服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lnmp ~]<span class="comment"># service nrpe restart</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;服务端也重启下 nagios 服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@nagios conf.d]<span class="comment"># service nagios restart</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再到浏览器刷新，会看到又多出来三个服务</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nagios%E7%9B%91%E6%8E%A7%E5%AE%A2%E6%88%B7%E7%AB%AF/08.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;稍微等一会就可以查看到具体的状态了。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nagios%E7%9B%91%E6%8E%A7%E5%AE%A2%E6%88%B7%E7%AB%AF/09.png?raw=true" alt=""></figure></p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Nagios/2. nagios 监控客户端/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
